Odoo / Coding / Server Action
Server Action
-
Step 1:
1. Create server action and call that action from your menu.
Any Name Goes Here True ir.actions.server code action=self.get_filtered_record(cr, uid, context.get('active_ids', []), context=context) 2. After that create a method or function under your model, and from that model after using your logic return action window.
def get_filtered_record(self):
Details:
-
Example:
Button
create a sales target model like below.
from odoo import models, fields, api, exceptions, _ class SalesTarget(models.Model): _name = 'ng.sale.target' name = fields.Char(default='Sales Target') target_line_id = fields.One2many('ng.sale.target.line', 'target_id') class SalesTargetLine(models.Model): _name = 'ng.sale.target.line' user_id = fields.Many2one('res.users', string='Sales Person') total_target = fields.Float(string='Total Target', default=0) target_id = fields.Many2one('ng.sale.target') Then make the form.
Sales Target ng.sale.target Next, create a menu item and an action server.
Sales Target ir.actions.server code action = model.open_sales_target() Finally add a method with the same name with the Action Server above to the model.
def open_sales_target(self): # check whether there is a sales target data or not existed_data = self.env['ng.sale.target'].search([],limit=1) # if no, create the new one if not existed_data: existed_data = self.create({}) # display the sales target data to the user using an Action Window return { 'type': 'ir.actions.act_window', 'view_type': 'form', 'view_mode': 'form', 'res_model': 'ng.sale.target', 'res_id': existed_data.id, }
Action Type: Where we define the type or category of the current action.
res_model, binding_model_id, binding_view_types, view_id, view_ids, view_mode, view_type, search_view_id, state, code, domain, context, target etc.
The available server actions are :
* code: Executes python code given in the argument of code. * object_create: It creates a new record. * object_write: Update the current record. * multi: Executes several actions given in the argument of child_ids.
Code example
Test Server Action list code action = model.action_test_code()
This will execute the method action_test_code() in the model test model.